package ws.jonas.common.model;

import ws.jonas.common.Util;

/**
 * Immutable.
 */
public class Product implements Comparable<Product> {
	private final int prodnr;
	private final String name;
	private final String volume;

	public Product(int prodnr, String name, String volume) {
		this.prodnr = prodnr;
		this.name = name;
		this.volume = volume;
	}

	public int getProdnr() {
		return prodnr;
	}

	public String getName() {
		return name;
	}

	public String getVolume() {
		return volume;
	}

	public String getUniqueName() {
		return name + " - " + volume;
	}

	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;
		result = prime * result + ((name == null) ? 0 : name.hashCode());
		result = prime * result + prodnr;
		result = prime * result + ((volume == null) ? 0 : volume.hashCode());
		return result;
	}

	@Override
	public boolean equals(Object obj) {
		if (this == obj) {
			return true;
		}
		if (obj == null) {
			return false;
		}
		if (!(obj instanceof Product)) {
			return false;
		}
		Product other = (Product) obj;
		if (name == null) {
			if (other.name != null) {
				return false;
			}
		} else if (!name.equals(other.name)) {
			return false;
		}
		if (prodnr != other.prodnr) {
			return false;
		}
		if (volume == null) {
			if (other.volume != null) {
				return false;
			}
		} else if (!volume.equals(other.volume)) {
			return false;
		}
		return true;
	}

	@Override
	public String toString() {
		return "Product [prodnr=" + prodnr + ", name=" + name + ", volume=" + volume + "]";
	}

	@Override
	public int compareTo(Product o) {
		if (this == o) {
			return 0;
		}
		if (o == null) {
			throw new NullPointerException();
		}
		int diff = prodnr - o.prodnr;
		if (diff != 0) {
			return diff;
		}
		diff = Util.compareNull(name, o.name);
		if (diff != 0) {
			return diff;
		}
		return Util.compareNull(volume, o.volume);
	}
}
